package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.geom.Point;
   import flash.geom.Rectangle;
   import flash.text.AntiAliasType;
   import flash.text.TextField;
   import flash.text.TextFieldType;
   import flash.text.TextFormat;
   import Jets.display.JContainer;
   
   
   public class JText extends JContainer
   {
	   
	   protected static var defautlfomt:TextFormat = new TextFormat("微软雅黑",12,0x0c212b);
	   
	   public var _tf:TextField;
	   protected var _text:String = "";
	   private var _editable:Boolean = true;
	   private var _usehtml:Boolean = false;
	   private var _margin:int = 2;
      
      public function JText(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, text:String="") 
	  {
         this._text = text;
         super(parent,cx,cy);
         setSize(200,100);
      }
      
    
      public function get margin() : int 
	  {
         return this._margin;
      }
      
      public function set margin(value:int) : void 
	  {
         this._margin = value;
         invalidate();
      }
      
      public function _getText() : String 
	  {
         return this._text;
      }
      
      override protected function init() : void 
	  {
         super.init();
      }
      
      override protected function addChildren() : void 
	  {
         this._tf = new TextField();
         this._tf.multiline = true;
         this._tf.wordWrap = true;
         this._tf.selectable = true;
         this._tf.defaultTextFormat = defautlfomt;
         this._tf.addEventListener(Event.CHANGE,this.oninput);
         this._tf.tabEnabled = false;
         addChild(this._tf);
      }
      
	  /**
	  * 设置使用html文本text
	  */ 
      public function set usehtml(bool:Boolean) : void 
	  {
         if(this._usehtml != bool)
         {
            this._usehtml = bool;
            this.invalidate();
         }
      }
      public function get usehtml() : Boolean 
	  {
         return this._usehtml;
      }
      
      public function get textWidth() : Number 
	  {
         return this._tf.textWidth;
      }
      
      public function set maxChars(value:int) : void 
	  {
         this._tf.maxChars = value;
      }
      
      public function get maxChars() : int 
	  {
         return this._tf.maxChars;
      }
      
      override public function draw() : void 
	  {
         super.draw();
         this._tf.x = this.margin;
         if(this is JTextArea)
         {
            this._tf.width = this.width - 12 - this.margin * 2;
         }
         else
         {
            this._tf.width = this.width - this.margin * 2;
         }
         this._tf.height = _height;
         if(this._editable)
         {
            this._tf.type = TextFieldType.INPUT;
         }
         else
         {
            this._tf.type = TextFieldType.DYNAMIC;
         }
         if(this._usehtml)
         {
            this._tf.htmlText = this._text;
         }
         else
         {
            if(this._tf.text != this._text)
            {
               this._tf.text = this._text;
            }
         }
      }
      
      public function set selectable(bool:Boolean) : void 
	  {
         this._tf.selectable = bool;
      }
      public function get selectable() : Boolean 
	  {
         return this._tf.selectable;
      }
      
      protected function oninput(event:Event) : void 
	  {
         if(this._editable)
         {
            this._text = this._tf.text;
         }
      }
      
      public function get textHeight() : Number 
	  {
         return this._tf.textHeight;
      }
   
      public function set text(txt:String) : void 
	  {
         var text:String = txt == null?"":txt;
         if(this._text != text)
         {
            this._text = text;
            if(this.usehtml == false)
            {
               this._tf.text = text;
               return;
            }
            invalidate();
         }
      }
      public function get text() : String 
	  {
         return this._tf.text;
      }
      
	  /**
	  * JText的data 方法就是设置text的内容
	  */ 
      override public function set data(value:Object) : void 
	  {
         this.text = value as String;
      }
      
      override public function get data() : Object 
	  {
         return this.text;
      }
      
	  /**
	  * 设置文本颜色
	  */ 
      public function set textColor(color:uint) : void 
	  {
         this._tf.textColor = color;
      }
      public function get textColor() : uint 
	  {
         return this._tf.textColor;
      }
      
	  /**
	  * 是否可以编辑
	  */ 
      public function set editable(bool:Boolean) : void 
	  {
         if(this._editable != bool)
         {
            this._editable = bool;
            if(this._editable == false)
            {
               this.usehtml = true;
            }
            invalidate();
         }
      }
      public function get editable() : Boolean 
	  {
         return this._editable;
      }
   }
}
